package com.cmrx.model.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cmrx.model.dao.SysdictDao;
import com.cmrx.model.entity.SysDict;
import com.cmrx.model.service.SysdictService;


@Service
@Transactional
public class SysdictServiceImpl implements SysdictService {

	@Autowired
	private SysdictDao sysdictDao;
	
	@Cacheable(value = "content",key="'casetype'")
	@Override
	public List<Map<String, Object>> selectCaseType() {
		String parentKey = "AJLBDM";
		List<Map<String, Object>> list = sysdictDao.selectCaseType(parentKey);
		for (Map<String, Object> map : list) {
			parentKey = map.get("dictKey").toString();
			map.put("selected", false);
			map.put("type", false);
			List<Map<String, Object>> list2 = sysdictDao.selectCaseType(parentKey);
			if(!list2.isEmpty()) {
				map.put("sonList", list2);
				for (Map<String, Object> map2 : list2) {
					parentKey = map2.get("dictKey").toString();
					map2.put("selected", false);
					map2.put("type", false);
					List<Map<String, Object>> list3 = sysdictDao.selectCaseType(parentKey);
					if(!list3.isEmpty()) {
						map2.put("sonList", list3);
						for (Map<String, Object> map3 : list3) {
							parentKey = map3.get("dictKey").toString();
							map3.put("selected", false);
							map3.put("type", false);
							List<Map<String, Object>> list4 = sysdictDao.selectCaseType(parentKey);
							if(!list4.isEmpty()) {
								map3.put("sonList", list4);
									for (Map<String, Object> map4 : list4) {
										map4.put("sonList", null);
										map4.put("selected", false);
										map4.put("type", false);
								}
							}else {
								map3.put("sonList", null);
							}
						}
					}else {
						map2.put("sonList", null);
					}
				}
			}else {
				map.put("sonList", null);
			}
		}
		return list;
	}
	
	@CachePut(value = "content",key="'casetype'")
	@Override
	public List<Map<String, Object>> dingshiCaseType() {
		String parentKey = "AJLBDM";
		List<Map<String, Object>> list = sysdictDao.selectCaseType(parentKey);
		for (Map<String, Object> map : list) {
			parentKey = map.get("dictKey").toString();
			map.put("selected", false);
			List<Map<String, Object>> list2 = sysdictDao.selectCaseType(parentKey);
			if(!list2.isEmpty()) {
				map.put("sonList", list2);
				for (Map<String, Object> map2 : list2) {
					parentKey = map2.get("dictKey").toString();
					map2.put("selected", false);
					List<Map<String, Object>> list3 = sysdictDao.selectCaseType(parentKey);
					if(!list3.isEmpty()) {
						map2.put("sonList", list3);
						for (Map<String, Object> map3 : list3) {
							parentKey = map3.get("dictKey").toString();
							map3.put("selected", false);
							List<Map<String, Object>> list4 = sysdictDao.selectCaseType(parentKey);
							if(!list4.isEmpty()) {
								map3.put("sonList", list4);
									for (Map<String, Object> map4 : list4) {
										map4.put("sonList", null);
										map4.put("selected", false);
									}
							}else {
								map3.put("sonList", null);
							}
						}
					}else {
						map2.put("sonList", null);
					}
				}
			}else {
				map.put("sonList", null);
			}
		}
		return list;
	}
	
	
	@Cacheable(value = "content",key="'gxsdm'")
	@Override
	public List<Map<String, Object>> selectgxsdm() {
		String parentKey = "GXSDM";
		List<Map<String, Object>> list = sysdictDao.selectgxsdm(parentKey);
		for (Map<String, Object> map : list) {
			parentKey = map.get("dictKey").toString();
			map.put("selected", false);
			List<Map<String, Object>> list2 = sysdictDao.selectgxsdm(parentKey);
			if(!list2.isEmpty()) {
				for (Map<String, Object> map2 : list2) {
					map2.put("selected", false);
				}
				map.put("sonList", list2);
			}else {
				map.put("sonList", null);
			}
		}
		return list;
	}
	
	
	@CachePut(value = "content",key="'gxsdm'")
	@Override
	public List<Map<String, Object>> dingshigxsdm() {
		String parentKey = "GXSDM";
		List<Map<String, Object>> list = sysdictDao.selectgxsdm(parentKey);
		for (Map<String, Object> map : list) {
			parentKey = map.get("dictKey").toString();
			map.put("selected", false);
			List<Map<String, Object>> list2 = sysdictDao.selectgxsdm(parentKey);
			if(!list2.isEmpty()) {
				for (Map<String, Object> map2 : list2) {
					map2.put("selected", false);
				}
				map.put("sonList", list2);
			}else {
				map.put("sonList", null);
			}
		}
		return list;
	}
	
	
	@Cacheable(value = "content",key="'sixcasetype'")
	@Override
	public List<Map<String, Object>> selectSixCaseType() {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		//盗三车
		Map<String, Object> map1 = new HashMap<String, Object>();
		String keys = "'050213','050228','050224'";
		map1.put("id", "");
		map1.put("dictLevel", "");
		map1.put("dictKey", "");
		map1.put("parentKey", "");
		map1.put("rootKey", "");
		map1.put("dictValue1", "盗三车");
		map1.put("dictPy", "");
		map1.put("type", "six");
		map1.put("sonList", sysdictDao.selectCaseTypeBykeys(keys));
		list.add(map1);
		Map<String, Object> map2 = new HashMap<String, Object>();
		keys = "'05021706','05021705','05021704','05021703','05021702','05021701'";
		map2.put("id", "");
		map2.put("dictLevel", "");
		map2.put("dictKey", "");
		map2.put("parentKey", "");
		map2.put("rootKey", "");
		map2.put("dictValue1", "扒窃拎包");
		map2.put("dictPy", "");
		map2.put("type", "six");
		map2.put("sonList", sysdictDao.selectCaseTypeBykeys(keys));
		list.add(map2);
		Map<String, Object> map3 = new HashMap<String, Object>();
		keys = "'050220','050234','050205'";
		map3.put("id", "");
		map3.put("dictLevel", "");
		map3.put("dictKey", "");
		map3.put("parentKey", "");
		map3.put("rootKey", "");
		map3.put("dictValue1", "露天空间");
		map3.put("dictPy", "");
		map3.put("type", "six");
		map3.put("sonList", sysdictDao.selectCaseTypeBykeys(keys));
		list.add(map3);
		Map<String, Object> map4 = new HashMap<String, Object>();
		keys = "'050310','050320','050308','050315','050319','050318','050327'";
		map4.put("id", "");
		map4.put("dictLevel", "");
		map4.put("dictKey", "");
		map4.put("parentKey", "");
		map4.put("rootKey", "");
		map4.put("dictValue1", "街面诈骗");
		map4.put("dictPy", "");
		map4.put("type", "six");
		map4.put("sonList", sysdictDao.selectCaseTypeBykeys(keys));
		list.add(map4);
		Map<String, Object> map5 = new HashMap<String, Object>();
		keys = "'060136','060137','04010310','04010309','04010308','04010307','04010306','04010305','04010304','04010303','04010302','04010301'";
		map5.put("id", "");
		map5.put("dictLevel", "");
		map5.put("dictKey", "");
		map5.put("parentKey", "");
		map5.put("rootKey", "");
		map5.put("dictValue1", "伤害斗殴");
		map5.put("dictPy", "");
		map5.put("type", "six");
		map5.put("sonList", sysdictDao.selectCaseTypeBykeys(keys));
		list.add(map5);
		Map<String, Object> map6 = new HashMap<String, Object>();
		keys = "'050902','050901'";
		map6.put("id", "");
		map6.put("dictLevel", "");
		map6.put("dictKey", "");
		map6.put("parentKey", "");
		map6.put("rootKey", "");
		map6.put("dictValue1", "破坏财物");
		map6.put("dictPy", "");
		map6.put("type", "six");
		map6.put("sonList", sysdictDao.selectCaseTypeBykeys(keys));
		list.add(map6);
		return list;
	}
	
	@CachePut(value = "content",key="'sixcasetype'")
	@Override
	public List<Map<String, Object>> dingshiSixCaseType() {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		//盗三车
		Map<String, Object> map1 = new HashMap<String, Object>();
		String keys = "'050213','050228','050224'";
		map1.put("id", "");
		map1.put("dictLevel", "");
		map1.put("dictKey", "");
		map1.put("parentKey", "");
		map1.put("rootKey", "");
		map1.put("dictValue1", "盗三车");
		map1.put("dictPy", "");
		map1.put("type", "six");
		map1.put("sonList", sysdictDao.selectCaseTypeBykeys(keys));
		list.add(map1);
		Map<String, Object> map2 = new HashMap<String, Object>();
		keys = "'05021706','05021705','05021704','05021703','05021702','05021701'";
		map2.put("id", "");
		map2.put("dictLevel", "");
		map2.put("dictKey", "");
		map2.put("parentKey", "");
		map2.put("rootKey", "");
		map2.put("dictValue1", "扒窃拎包");
		map2.put("dictPy", "");
		map2.put("type", "six");
		map2.put("sonList", sysdictDao.selectCaseTypeBykeys(keys));
		list.add(map2);
		Map<String, Object> map3 = new HashMap<String, Object>();
		keys = "'050220','050234','050205'";
		map3.put("id", "");
		map3.put("dictLevel", "");
		map3.put("dictKey", "");
		map3.put("parentKey", "");
		map3.put("rootKey", "");
		map3.put("dictValue1", "露天空间");
		map3.put("dictPy", "");
		map3.put("type", "six");
		map3.put("sonList", sysdictDao.selectCaseTypeBykeys(keys));
		list.add(map3);
		Map<String, Object> map4 = new HashMap<String, Object>();
		keys = "'050310','050320','050308','050315','050319','050318','050327'";
		map4.put("id", "");
		map4.put("dictLevel", "");
		map4.put("dictKey", "");
		map4.put("parentKey", "");
		map4.put("rootKey", "");
		map4.put("dictValue1", "街面诈骗");
		map4.put("dictPy", "");
		map4.put("type", "six");
		map4.put("sonList", sysdictDao.selectCaseTypeBykeys(keys));
		list.add(map4);
		Map<String, Object> map5 = new HashMap<String, Object>();
		keys = "'060136','060137','04010310','04010309','04010308','04010307','04010306','04010305','04010304','04010303','04010302','04010301'";
		map5.put("id", "");
		map5.put("dictLevel", "");
		map5.put("dictKey", "");
		map5.put("parentKey", "");
		map5.put("rootKey", "");
		map5.put("dictValue1", "伤害斗殴");
		map5.put("dictPy", "");
		map5.put("type", "six");
		map5.put("sonList", sysdictDao.selectCaseTypeBykeys(keys));
		list.add(map5);
		Map<String, Object> map6 = new HashMap<String, Object>();
		keys = "'050902','050901'";
		map6.put("id", "");
		map6.put("dictLevel", "");
		map6.put("dictKey", "");
		map6.put("parentKey", "");
		map6.put("rootKey", "");
		map6.put("dictValue1", "破坏财物");
		map6.put("dictPy", "");
		map6.put("type", "six");
		map6.put("sonList", sysdictDao.selectCaseTypeBykeys(keys));
		list.add(map6);
		return list;
	}
	
	/*@Cacheable(value = "content",key="#rootKey")
	@Override
	public List<Map<String, Object>> selectSysdict(String rootKey) {
		String parentKey = rootKey;
		List<Map<String, Object>> list = sysdictDao.selectSysdict(parentKey, rootKey);
		for (Map<String, Object> map : list) {
			parentKey = map.get("dictKey").toString();
			List<Map<String, Object>> list2 = sysdictDao.selectSysdict(parentKey, rootKey);
			map.put("sonList", list2);
			if(!list2.isEmpty()) {
				for (Map<String, Object> map2 : list2) {
					parentKey = map2.get("dictKey").toString();
					List<Map<String, Object>> list3 = sysdictDao.selectSysdict(parentKey, rootKey);
					map2.put("sonList", list3);
				}
			}
		}
		return list;
	}*/


    @Cacheable(value = "content",key="#rootKey")
    @Override
    public List<Map<String, Object>> selectSysdict(String rootKey) {
        String parentKey = rootKey;
        List<Map<String, Object>> list = sysdictDao.selectSysdict(parentKey, rootKey);
        list = selectDiGui(rootKey,list);
        return list;
    }

	public List<Map<String, Object>> selectDiGui(String rootKey,List<Map<String, Object>> list) {
        for (Map<String, Object> map : list) {
            String parentKey = map.get("dictKey").toString();
            List<Map<String, Object>> list2 = sysdictDao.selectSysdict(parentKey, rootKey);
            map.put("sonList", list2);
            if(!list2.isEmpty()) {
                selectDiGui(rootKey,list2);
            }
        }
        return  list;
    }





    @Override
    public List<Map<String, Object>> selectSysdictByDictId(String rootKey, String dict_id) {
        List<Map<String, Object>> list = sysdictDao.selectSysdictByDictId(dict_id,rootKey);
        return list;
    }

    @CachePut(value = "content",key="#rootKey")
	@Override
	public List<Map<String, Object>> dingshiSysdict(String rootKey) {
		String parentKey = rootKey;
		List<Map<String, Object>> list = sysdictDao.selectSysdict(parentKey, rootKey);
		for (Map<String, Object> map : list) {
			parentKey = map.get("dictKey").toString();
			List<Map<String, Object>> list2 = sysdictDao.selectSysdict(parentKey, rootKey);
			map.put("sonList", list2);
			if(!list2.isEmpty()) {
				for (Map<String, Object> map2 : list2) {
					parentKey = map2.get("dictKey").toString();
					List<Map<String, Object>> list3 = sysdictDao.selectSysdict(parentKey, rootKey);
					map2.put("sonList", list3);
				}
			}
		}
		return list;
	}

//	@CachePut(value = "content",key="#rootKey + #parentKey")
	@Override
	public List<Map<String, Object>> selectOneCaseType(String rootKey, String parentKey) {
        List<Map<String, Object>> list = sysdictDao.selectSysdict(parentKey, rootKey);
        for (Map<String, Object> map:list) {
            String parentKeyParam = map.get("dictKey").toString();
            int count = sysdictDao.selectSysdictCount(parentKeyParam,rootKey);
            if(count>0){
                map.put("sonType",true);
            }else {
                map.put("sonType",false);
            }
        }
		return list;
	}

	// 根据物证rootKey查询物证类型
	@CachePut(value = "content",key="#rootKey")
	@Override
	public List<Map<String, Object>> selectEvidenceType(String rootKey) {
		return sysdictDao.selectEvidenceType(rootKey);
	}


}
